package cn.peach.user.controller;

import cn.peach.basic.exception.MyException;
import cn.peach.basic.util.AjaxResult;
import cn.peach.user.dto.LoginDto;
import cn.peach.user.service.ILoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private ILoginService loginService;

    /**
     * 登录
     * @param loginDto
     * @return
     */
    @PostMapping("/account")
    public AjaxResult account(@RequestBody LoginDto loginDto){
        try {
            Map<String, Object> map = loginService.account(loginDto);
            // 把map返回给浏览器端
            return AjaxResult.me().subResultObj(map);
        } catch (MyException e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统没了");
        }
    }

    /**
     * 微信登录
     * @param loginDto
     * @return
     */
    @PostMapping("/wechat")
    // 直接返回一个AjaxResult对象
    public AjaxResult wechat(@RequestBody Map<String,String> params){
        try {
            return loginService.wechat(params);
        } catch (MyException e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setMessage("微信登陆系统异常");
        }
    }

    /**
     * 微信绑定
     * @param param
     * @return
     */
    @PostMapping("/binder/wechat")
    // 直接返回一个AjaxResult对象
    public AjaxResult binder(@RequestBody Map<String,String> param){
        try {
            Map<String,Object> map = loginService.binder(param);
            return AjaxResult.me().subResultObj(map);
        } catch (MyException e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setMessage("微信绑定系统异常");
        }
    }
}
